{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook will show a quick example to create a reference_dem object.\n",
    "\n",
    "First, import the module. I added this step via an explicit path for testing now, but it will be updated later."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import the module via an explicit path directly for now\n",
    "import importlib.util\n",
    "spec = importlib.util.spec_from_file_location(\"coregistration\", \"/home/jovyan/Assimilation/scripts/coregistration.py\")\n",
    "cor = importlib.util.module_from_spec(spec)\n",
    "spec.loader.exec_module(cor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This module defines a reference_dem class. With the reference_dem class, we can create an object which holds all of the info we are interested in. Namely, the DEM itself, plus the bounding box, the projection, the list of ATL06 files which pertain to the file, the bare rock pixels, etc. THe object would also have methods to generate pertinent info.\n",
    "\n",
    "To initiate the object using the coregistration code, one would only need to identify the location of the file. This is the Baker DEM file provided by Friedrich Knuth:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "dem_file_path = '/home/jovyan/data/reference_dem_clip.tif'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initiate the object as:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = cor.reference_dem(dem_file_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This object contains the data of the DEM as well as the projection info:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x shape:  (6599,)\n",
      "y shape:  (8157,)\n",
      "dem shape:  (8157, 6599)\n",
      "bounding box ([min_x,min_y,max_x,max_y]):\n",
      "      [582482.8448944142, 5395092.518148141, 588510.3221990903, 5402543.276693823]\n",
      "projection (EPSG):  32610\n",
      "bounding box projection (EPSG):  32610\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print('x shape: ',np.shape(ds.x))\n",
    "print('y shape: ',np.shape(ds.y))\n",
    "print('dem shape: ',np.shape(ds.dem))\n",
    "print('bounding box ([min_x,min_y,max_x,max_y]):')\n",
    "print('     ',ds.bbox)\n",
    "print('projection (EPSG): ',ds.epsg)\n",
    "print('bounding box projection (EPSG): ',ds.bbox_epsg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the bounding box has its own epsg attribute. The bounding box can be reprojected by calling the `calculate_bounding_box` function and specifying a new EPSG code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bounding box ([min_x,min_y,max_x,max_y]):\n",
      "      [-121.87893182746436, 48.70258466806347, -121.79542720391288, 48.7704252035505]\n",
      "bounding box projection (EPSG):  4326\n"
     ]
    }
   ],
   "source": [
    "ds.calculate_bounding_box(epsg=4326)\n",
    "\n",
    "print('bounding box ([min_x,min_y,max_x,max_y]):')\n",
    "print('     ',ds.bbox)\n",
    "print('bounding box projection (EPSG): ',ds.bbox_epsg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It can also be reprojected back to the original reference system:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bounding box ([min_x,min_y,max_x,max_y]):\n",
      "      [5395092.518148141, 582482.8448944142, 5402543.276693823, 588510.3221990903]\n",
      "bounding box projection (EPSG):  32610\n"
     ]
    }
   ],
   "source": [
    "ds.calculate_bounding_box(epsg=ds.epsg)\n",
    "\n",
    "print('bounding box ([min_x,min_y,max_x,max_y]):')\n",
    "print('     ',ds.bbox)\n",
    "print('bounding box projection (EPSG): ',ds.bbox_epsg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
